<template>
  <view class="container">
    <view class="header">
      <text class="title">TabBar页面路径设置</text>
      <text class="subtitle">配置小程序底部导航栏的页面路径和显示内容</text>
    </view>
    
    <view class="content">
      <!-- 左侧：手机预览 -->
      <view class="left-panel">
        <view class="phone-container">
          <view class="phone-header">
            <text class="phone-title">手机预览</text>
            <text class="operate-tip">点击下方TabBar切换配置</text>
          </view>
          <view class="phone-screen">
            <view class="status-bar">
              <text>9:41</text>
              <text>📶 100% 🔋</text>
            </view>
            
            <view class="page-content">
              <view class="page-content-area">
                <view class="preview-placeholder">
                  <text class="preview-text">页面预览区域</text>
                  <text class="preview-hint">选择TabBar后显示对应页面</text>
                </view>
              </view>
            </view>
            
            <view class="tab-bar-area">
              <view class="tab-bar-tip">
                <text class="tip-icon">💡</text>
                <text class="tip-text">点击下方TabBar项目进行配置</text>
              </view>
              <view class="tab-bar">
                <view 
                  class="tab-bar-item" 
                  v-for="(tab, index) in tabBarList" 
                  :key="index"
                  :class="{ active: activeTabIndex === index }"
                  @tap="switchTab(index)"
                >
                  <view class="tab-icon-wrapper">
                    <image 
                      class="tab-icon" 
                      :src="tab.iconPath || '/static/default-icon.png'" 
                      mode="aspectFit"
                    ></image>
                    <view class="edit-badge" v-if="activeTabIndex === index">编辑中</view>
                  </view>
                  <text class="tab-text">{{ tab.text }}</text>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
      
      <!-- 右侧：配置面板 -->
      <view class="right-panel" v-if="pageOptions.length>0 && activeTabIndex>=0">
        <view class="config-panel">
          <view class="panel-header">
            <view class="panel-title">TabBar配置</view>
            <view class="current-tab">
              <text class="current-label">当前编辑：</text>
              <text class="current-name">{{ tabBarList[activeTabIndex].text }}</text>
            </view>
          </view>
          
          <view class="config-content">
            <view class="config-section">
              <view class="section-title">
                <text>页面设置</text>
              </view>
              <view class="form-group">
                <view class="form-label">
                  <text>选择页面</text>
                  <text class="label-desc">设置该TabBar对应的页面路径</text>
                </view>
                <picker 
                  class="path-selector" 
                  :value="selectedPageIndex" 
                  :range="pageOptions" 
                  range-key="name"
                  @change="onPageChange"
                >
                  <view class="picker-display">
                    <text class="picker-text">
                      {{ pageOptions[selectedPageIndex] ? pageOptions[selectedPageIndex].name : '请选择页面' }}
                    </text>
                    <text class="picker-arrow">▼</text>
                  </view>
                </picker>
              </view>
            </view>

            <view class="config-section">
              <view class="section-title">
                <text>显示设置</text>
              </view>
              
              <view class="form-group">
                <view class="form-label">
                  <text>页面标题</text>
                  <text class="label-desc">页面顶部的标题内容</text>
                </view>
                <input 
                  class="form-input" 
                  @input="update(selectedPageIndex)" 
                  placeholder="请输入页面标题" 
                  placeholder-class="placeholder-class" 
                  v-model="pageOptions[selectedPageIndex].name"
                />
              </view>
              
              <view class="form-group">
                <view class="form-label">
                  <text>TabBar标题</text>
                  <text class="label-desc">限制4个字以内</text>
                </view>
                <input 
                  class="form-input" 
                  @input="update(selectedPageIndex)" 
                  placeholder="请输入TabBar标题" 
                  placeholder-class="placeholder-class" 
                  v-model="pageOptions[selectedPageIndex].text"
                  maxlength="4"
                />
                <view class="char-count">
                  <text>{{ pageOptions[selectedPageIndex].text ? pageOptions[selectedPageIndex].text.length : 0 }}/4</text>
                </view>
              </view>
            </view>
            
            <view class="action-buttons">
              <button class="btn cancel-btn" @tap="cancelEdit">取消</button>
              <button class="btn save-btn" @tap="tabbar_SetMy">保存设置</button>
            </view>
          </view>
        </view>
      </view>

      <!-- 未选择时的提示 -->
      <view class="right-panel" v-else>
        <view class="empty-state">
          <view class="empty-icon">👆</view>
          <text class="empty-title">请选择要配置的TabBar</text>
          <text class="empty-desc">点击左侧手机预览中的TabBar项目开始配置</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      activeTabIndex: -1,
      selectedPageIndex: 0,
      tabBarList: [
        { text: "首页" },
        { text: "分类" },
        { text: "购物车" },
        { text: "我的" }
      ],
      pageOptions: []
    }
  },
  created() {
    this.GetTabBar()
    this.tabbar_GetMy()
  },
  methods: {
    GetTabBar() {
      var that = this
      this.$common.get_request(
        {type:'tabbar_Get'},
        (res) => {
          that.pageOptions = []
          that.pageOptions.push({name:'请选择',text:'请选择'});
          that.pageOptions = [...res.list]
        }
      );
    },
    tabbar_GetMy() {
      var that = this
      this.$common.get_request(
        {type:'tabbar_GetMy'},
        (res) => {
          that.tabBarList = res.list
        }
      );
    },
    tabbar_SetMy() {
      var that = this
      for(var i=0;i<that.tabBarList.length;i++){
        if(that.tabBarList[i].index==undefined || that.tabBarList[i].name==undefined || that.tabBarList[i].selectedIconPath==undefined){
          that.$common.showModal('提示', "需要全部栏目设置完毕后方可保存")
          return
        }
      }
      this.$common.get_request(
        {
          type:'tabbar_SetMy',
          data:JSON.stringify(that.tabBarList)
        },
        (res) => {
          this.activeTabIndex = -1
          that.$common.showModal('提示',res.msg)
        }
      );
    },
    onPageChange(e) {
      var index = parseInt(e.detail.value)
      this.update(index)
    },
    update(index){
      var item = JSON.parse(JSON.stringify(this.pageOptions[index]))
      item.index = this.activeTabIndex
      this.$set(this.tabBarList,this.activeTabIndex,item)
      this.tabBarList[this.activeTabIndex] = item
      this.selectedPageIndex = index
    },
    switchTab(index) {
      this.activeTabIndex = index
      this.selectedPageIndex = 0
    },
    cancelEdit() {
      this.activeTabIndex = -1
    }
  }
}
</script>

<style scoped>
.container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background: #f5f5f7;
  overflow: hidden;
}

.header {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
  background: white;
  border-bottom: 1px solid #e1e1e1;
}

.title {
  font-size: 24px;
  font-weight: bold;
  color: #1a1a1a;
  margin-bottom: 8px;
}

.subtitle {
  font-size: 14px;
  color: #666;
}

.content {
  display: flex;
  flex: 1;
  min-height: 0;
  padding: 20px;
  gap: 20px;
}

/* 左侧面板 */
.left-panel {
  display: flex;
  justify-content: center;
  flex-direction: column;
}

.phone-container {
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  padding: 20px;
	flex: 1;
	flex-direction: column;
	display: flex;
}

.phone-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.phone-title {
  font-size: 18px;
  font-weight: 600;
  color: #1a1a1a;
}

.operate-tip {
  font-size: 12px;
  color: #ff6b35;
  background: #fff0e8;
  padding: 4px 8px;
  border-radius: 12px;
}

.phone-screen {
  width: 360px;
  height: 100%;
  background: #f8f9fa;
  border-radius: 16px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  border: 2px solid #e1e1e1;
}

.status-bar {
  height: 40px;
  background: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  font-size: 14px;
  color: #333;
  border-bottom: 1px solid #eee;
}

.page-content {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.page-content-area {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.preview-placeholder {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: white;
  margin: 20px;
  border-radius: 12px;
  border: 2px dashed #e1e1e1;
}

.preview-text {
  font-size: 16px;
  color: #666;
  margin-bottom: 8px;
}

.preview-hint {
  font-size: 12px;
  color: #999;
}

.tab-bar-area {
  background: white;
  border-top: 1px solid #eaeaea;
}

.tab-bar-tip {
  display: flex;
  align-items: center;
  justify-content: center;
  background: #e8f4ff;
  padding: 8px;
  gap: 6px;
}

.tip-icon {
  font-size: 14px;
}

.tip-text {
  font-size: 12px;
  color: #1890ff;
}

.tab-bar {
  height: 60px;
  display: flex;
  padding: 0 10px;
}

.tab-bar-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: #666;
  transition: all 0.2s;
  position: relative;
}

.tab-bar-item.active {
  color: #1890ff;
}

.tab-bar-item.active::before {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 30px;
  height: 3px;
  background: #1890ff;
  border-radius: 0 0 3px 3px;
}

.tab-icon-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 4px;
}

.tab-icon {
  width: 24px;
  height: 24px;
}

.edit-badge {
  position: absolute;
  top: -8px;
  right: -8px;
  background: #ff6b35;
  color: white;
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 8px;
  white-space: nowrap;
}

.tab-text {
  font-size: 12px;
  font-weight: 500;
}

/* 右侧面板 */
.right-panel {
  width: 400px;
  display: flex;
  flex-direction: column;
}

.config-panel {
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  display: flex;
  flex-direction: column;
  height: 100%;
}

.panel-header {
  padding: 24px;
  border-bottom: 1px solid #f0f0f0;
  display: flex;
}

.panel-title {
  font-size: 20px;
  font-weight: 600;
  color: #1a1a1a;
}

.current-tab {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

.current-label {
  font-size: 14px;
  color: #666;
}

.current-name {
  font-size: 16px;
  font-weight: 600;
  color: #1890ff;
}

.config-content {
  flex: 1;
  padding: 24px;
  display: flex;
  flex-direction: column;
      overflow: auto;
}

.config-section {
  margin-bottom: 32px;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #1a1a1a;
  margin-bottom: 16px;
  padding-bottom: 8px;
  border-bottom: 1px solid #f0f0f0;
}

.form-group {
  margin-bottom: 20px;
}

.form-label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 8px;
}

.form-label text:first-child {
  font-size: 14px;
  font-weight: 500;
  color: #333;
}

.label-desc {
  font-size: 12px;
  color: #999;
}

.path-selector {
  width: 100%;
}

.picker-display {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border: 1px solid #e1e1e1;
  border-radius: 8px;
  background: white;
}

.picker-text {
  font-size: 14px;
  color: #333;
}

.picker-arrow {
  font-size: 12px;
  color: #999;
}

.form-input {
  padding: 12px 16px;
  border: 1px solid #e1e1e1;
  border-radius: 8px;
  font-size: 14px;
  color: #333;
  background: white;
}

.form-input:focus {
  border-color: #1890ff;
}

.char-count {
  text-align: right;
  font-size: 12px;
  color: #999;
  margin-top: 4px;
}

.placeholder-class {
  color: #ccc;
}

.action-buttons {
  display: flex;
  gap: 12px;
  margin-top: auto;
  padding-top: 20px;
}

.btn {
  flex: 1;
  height: 44px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.cancel-btn {
  background: #f5f5f5;
  color: #666;
  border: 1px solid #e1e1e1;
}

.save-btn {
  background: #1890ff;
  color: white;
}

.save-btn:active {
  background: #096dd9;
}

/* 空状态 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  padding: 40px;
  text-align: center;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.empty-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.empty-desc {
  font-size: 14px;
  color: #666;
  line-height: 1.5;
}
</style>